<?php

namespace campus\controllers;

use common\cache\DictCache;
use common\helpers\commonApi;
use common\helpers\WeChatApi;
use common\models\Campus;
use common\models\service\WithdrawalService;
use common\models\System;
use common\models\User;
use common\models\Withdrawal;
use Yii;
use yii\helpers\ArrayHelper;

class WithdrawalController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Withdrawal::className();
    }

    /**
     * 列表
     * @return array|string
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $fund_type = $post['fund_type']??'';
            return WithdrawalService::backList($fund_type);
        }
        return parent::actionIndex();
    }

    /**
     * 审核
     * @return array|bool|string
     */
    public function actionReview()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            return WithdrawalService::review($post);
        }
        $data = WithdrawalService::review([]);
        return $this->render('',$data);
    }

    public function actionMyList()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex(true,[['campus_id','=',$this->campus_id],['fund_type','=',3]]);
            $review_status = DictCache::get('review_status');
            $ithdraw_way = DictCache::get('withdraw_way');
            $userids = array_unique(ArrayHelper::getColumn($data['data'], 'userid'));
            $userInfo = (new User())->getUserHtml($userids);
            foreach ($data['data'] as &$datum){
                $datum['review_status_title'] = $review_status[$datum['review_status']]??'';
                $datum['way_title'] = $ithdraw_way[$datum['way']]??'';
                $datum['user_html'] = $userInfo[$datum['userid']]??'';
                $datum['payment_amount'] = $datum['amount']-$datum['fee'];
            }
            return $data;
        }
        $review_status = DictCache::get('review_status');
        $model = Campus::findOne($this->campus_id);
        $can_brokerage = $model->can_brokerage??0;
        return $this->render('',['can_brokerage'=>$can_brokerage,'review_status'=>$review_status]);
    }

    public function actionApply()
    {
        $model = Campus::findOne($this->campus_id);
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if (!$model){
                return commonApi::message('信息错误',false);
            }
            if (!$model->payment_userid){
                return commonApi::message('请绑定收款人微信',false);
            }
            if (empty($post['amount'])){
                return commonApi::message('请输入提现金额',false);
            }
            $data = [
                'amount'=>$post['amount'],
                'fund_type'=>3,
                'way'=>2,
                'campus_id_id'=>$model->id,
            ];
            return (new Withdrawal())->apply($model->payment_userid,$data);
        }
        if (!$model){
            return $this->bError('信息错误');
        }
        if ($model->payment_userid){
            $user = (new User())->getUserHtml($model->payment_userid);
            $userHtml = $user[$model->payment_userid]??'';
        }else{
            $config = System::applets();
            $wx = new WeChatApi($config);
            $result = $wx->appletsQr('pages/index/index','campus_id_'.$this->campus_id);
            if (!$result['success']){
                return $this->bError('请联系客服，出现小程序二维码错误');
            }
            $userHtml = "<div><img src='{$result['data']['img']}' alt='' style='width: 200px;'><div>注：请绑定微信，在绑定之前请先删除【此小程序】，重新扫码打开进行【登录】</div></div>";
        }
        $fee = System::getModel()->ca_withdraw_fee;
        return $this->render('',['userHtml'=>$userHtml,'model'=>$model,'fee'=>$fee]);
    }
}